#include "ATan.h"
#include "Cos.h"
#include "Putere.h"
#include "Plus.h"
#include "Impartit.h"

long double ATan::calcul(long double aux1)
{
    return atanl(aux1);
}

long double ATan::calcul()
{
    if (!fStang)
        exit(1);
    return calcul(fStang->calcul());
}

char *ATan::derivare()
{
    char *aux1, *aux2, *aux, *aux00, *aux01;

    if (!fStang)
        exit(1);

    aux1 = fStang->derivare();
    aux2 = fStang->toString();

    aux00 = Putere::concatTermeni(aux2,(char*)"2");
    aux01 = Plus::concatTermeni((char*)"1", aux00);
    aux = Impartit::concatTermeni(aux1, aux01);
    delete aux1; delete aux2; delete aux00; delete aux01;

    return aux;
}

long double ATan::calculDerivata()
{
    if (!fStang)
        exit(1);

    return Impartit::calcul(fStang->calculDerivata(), Plus::calcul(1,
            Putere::calcul(fStang->calcul(), 2)));
}

char *ATan::toString()
{
    char *aux, *aux1;
    int nrP;

    if (!fStang)
        exit(1);
    aux1 = fStang->toString();

    aux = concatTermeni(aux1);
    delete aux1;
    return aux;
}

char *ATan::concatTermeni(char *aux1)
{
    char *aux;
    int nrP;

    nrP = aux1[0] == '(' ? 0 : 2;
    aux = new char[strlen(aux1) + 5 + nrP];

    strcpy(aux, "atan");
    if (nrP) strcat(aux, "(");
    strcat(aux, aux1);
    if (nrP) strcat(aux, ")");

    return aux;
}
